﻿
#<# File:MutexVerifier.ps1
# This script contains functions that are used to 
# manage the execution of verifier based on the resolver ran
##>

# Function to be called from the troubleshooter script
# Create a unique file for logging purpose
function CreateUniqueGlobalFileName([string]$filename){
		
	$global:filename = $env:temp + "\$filename" 
	$global:resolverRan =  $env:temp + "\$filename"+"_resolverRan"
	$global:resolverFail =  $env:temp + "\$filename" + "_resolverFail"

	if(test-path ($global:resolverRan)){
		del $global:resolverRan
	}

	if(test-path ($global:filename)){
		del $global:filename
	}

	if(test-path ($global:resolverFail)){
		del $global:resolverFail
	}
}

# Function to be called from the verifier script
# Usage: if(checkResolver "AppSettings" $id) { return }
function checkResolver {
	 param([string]$res ,
	 [string]$instanceid = $null,
	 $params = $null) 

	# pop-msg ($res+","+$instanceid)
	$resRunning = ""
	if(test-path  $global:filename){
		$resRunning = cat $global:filename
	}

	$alreadyRan = ""
	if(test-path  $global:resolverRan){
		$alreadyRan = cat $global:resolverRan
	}

	$resFailed = $null

	if(test-path $global:resolverFail){
		$resFailed = cat $global:resolverFail
	}

	$detected = $true

	$returnFromFailed = $true;
	
	#Check for failed resolvers
	if($resfailed -ne $null){
	foreach($f in $resFailed){
		
		$str = $f.split(',')
		
		$res = $str[0]

		if($str[1] -eq ""){
			$instanceid = $null
		}

		if( ($null -eq $instanceid) -and ($null -eq $params) ){ # if both parameters and instance id are null

		
			update-diagrootcause -id "RC_$res" -detected $true
		
		}elseif( ($null -eq $instanceid) ){ # if only instance id is null
	
			update-diagrootcause -id "RC_$res" -detected $true -parameter $params

		}elseif( ($null -eq $params) ){ #if only parameters is null
		
			if("$res,$instanceid" -eq $f){
				update-diagrootcause -id "RC_$res" -detected $true -iid $instanceid 
				
			}else{
				$returnFromFailed = $false
			}

		}else{ # if both parameter and instance id are not null
		
			if("$res,$instanceid" -eq $f){
				update-diagrootcause -id "RC_$res" -detected $true -iid $instanceid -parameter $params 
			}else{
				$returnFromFailed = $false
			}

		}

		if($returnFromFailed) { return $true }
	}
	}
	
	# Keep the list of resolvers already ran 
	foreach($ran in $alreadyRan){
		$ran=[string] $ran
		$tallyString = $res + "," + $instanceid
		if( ($ran.length -eq $tallyString.length) -and ($tallystring.indexof($ran) -eq 0) ){
			return $false
		}
	}


	# Check whether the current resolver is of current verifier	
	if( ($resRunning[0] -eq $res) -and ($resRunning[1] -eq $instanceid) ){
		return $false

	}elseif( ($null -eq $instanceid) -and ($null -eq $params) ){ # if both parameters and instance id are null

		
		update-diagrootcause -id "RC_$res" -detected $true
		
	}elseif( ($null -eq $instanceid) ){ # if only instance id is null
		
		update-diagrootcause -id "RC_$res" -detected $true -parameter $params

	}elseif( ($null -eq $params) ){ #if only parameters is null
	
		update-diagrootcause -id "RC_$res" -detected $true -iid $instanceid 

	}else{ # if both parameter and instance id are not null
		update-diagrootcause -id "RC_$res" -detected $true -iid $instanceid -parameter $params 

	}

	return $detected
}


# Function to be called from resolver script
# Usage: runningResolver "AppSettings" $id
function runningResolver {

	 param($resolverName ,
	 $instanceid = $null)

	($resolvername) > $global:filename

	if($null -ne $instanceid){
		$instanceid >> $global:filename
	}else{
		"" >> $global:filename
	}

	ResolverRan ($resolverName) ($instanceid)
}


# Private funtion to keep the list of resolvers already ran 
function ResolverRan {

	 param($resolverName ,
	 $instanceid = $null)	
	($resolvername+","+$instanceid) >> $global:resolverRan

}

